import { defineStore } from 'pinia';
import Cookies from 'js-cookie';

/**
 * 应用存储定义
 */
const useAppStore = defineStore(
  'app',
  {
    state: () => ({
      /**
       * 侧边栏状态
       */
      sidebar: {
        opened: Cookies.get('sidebarStatus') ? Cookies.get('sidebarStatus') : true,
        withoutAnimation: false,
        hide: false
      },
      /**
       * 设备类型
       */
      device: 'desktop' as 'desktop' | 'mobile',
      /**
       * 界面尺寸
       */
      size: (Cookies.get('size') || 'default')
    }),
    actions: {
      /**
       * 切换侧边栏打开/关闭状态
       * @param withoutAnimation - 是否不使用动画
       */
      toggleSideBar(withoutAnimation: boolean) {
        if (this.sidebar.hide) {
          return false;
        }
        this.sidebar.opened = !this.sidebar.opened;
        this.sidebar.withoutAnimation = withoutAnimation;
        if (this.sidebar.opened) {
          Cookies.set('sidebarStatus', '1');
        } else {
          Cookies.set('sidebarStatus', '0');
        }
      },
      /**
       * 关闭侧边栏
       * @param withoutAnimation - 是否不使用动画
       */
      closeSideBar({ withoutAnimation }: { withoutAnimation: boolean }) {
        Cookies.set('sidebarStatus', '0');
        this.sidebar.opened = false;
        this.sidebar.withoutAnimation = withoutAnimation;
      },
      /**
       * 切换设备类型
       * @param device - 设备类型
       */
      toggleDevice(device: 'desktop' | 'mobile') {
        this.device = device;
      },
      /**
       * 设置界面尺寸
       * @param size - 尺寸类型
       */
      // setSize(size: 'default' | 'large' | 'small') {
      setSize(size:string) {
        this.size = size;
        Cookies.set('size', size);
      },
      /**
       * 切换侧边栏隐藏状态
       * @param status - 隐藏状态
       */
      toggleSideBarHide(status: boolean) {
        this.sidebar.hide = status;
      }
    }
  });

export {useAppStore};